//
//  WYStatusTableViewCell.swift
//  WYWeibo
//
//  Created by wangyi on 16/7/14.
//  Copyright © 2016年 wangyi. All rights reserved.
//

import UIKit

import SnapKit

/*

    根据分析  需要 自定义cell  
    cell 分为单三个板块 : 头部 view
                     : 中间转发view 
                     : 下面点击视图

*/

//首页 Cell 子控件的间距
let HomeCellMargin: CGFloat = 10

class WYStatusTableViewCell: UITableViewCell {
    
    var toolbarTopConstraint: Constraint?
    var statusViewModel: WYStatusViewModel? {
        
        didSet {
            //卸载约束
            toolbarTopConstraint?.uninstall()
            //给原创微博视图设置需要的视图 模式
            original.statusViewModel = statusViewModel
            //判断是否有转发对象
            if statusViewModel?.status?.retweeted_status != nil {
            
                retweet.statusViewModel = statusViewModel
                
                retweet.hidden = false
                
                toolBar.snp_makeConstraints(closure: { (make) -> Void in
                    //记录跟新新的约束
                    self.toolbarTopConstraint =  make.top.equalTo(retweet.snp_bottom).constraint
                })
                
            } else {
            
                //没有转发微博数组 隐藏微博视图 跟新数据
                retweet.hidden = true
                toolBar.snp_makeConstraints(closure: { (make) -> Void in
                    self.toolbarTopConstraint = make.top.equalTo(original.snp_bottom).constraint
                })
            }
            //给 tabar 视图赋值
            toolBar.statusViewModel = statusViewModel
        }
        
    }
    
    
    //MARK -- 懒加载视图
    //自定义视图
    private lazy var original: WYStatusOriginalView = WYStatusOriginalView()
    //转发视图
    private lazy var retweet: WYStatusRetweetView = WYStatusRetweetView()
    //按钮 button
    private lazy var toolBar: WYStatusToolBar = WYStatusToolBar()
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }
    //重写 cell
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)

        //调用
        setUPUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setUPUI() {
        
        //添加视图
        contentView.addSubview(original)
        contentView.addSubview(toolBar)
        contentView.addSubview(retweet)
        //设置坐标
        original.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentView).offset(8)
            make.leading.equalTo(contentView)
            make.trailing.equalTo(contentView)
        }
        retweet.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(original.snp_bottom)
            make.leading.equalTo(original)
            make.trailing.equalTo(original)
            
        }
        toolBar.snp_makeConstraints { (make) -> Void in
            self.toolbarTopConstraint = make.top.equalTo(retweet.snp_bottom).constraint
            make.height.equalTo(35)
            make.leading.equalTo(contentView)
            make.trailing.equalTo(contentView)
        }
        
        //非常重要的约束 确定 cell 的高度  和 tools 底部相同
        contentView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(self)
            make.trailing.equalTo(self)
            make.top.equalTo(self)
            make.bottom.equalTo(toolBar.snp_bottom)
        }
        
    }
}
